package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.Department;
import com.ruoyi.system.domain.Doctor;
import com.ruoyi.system.domain.Hospital;
import com.ruoyi.system.domain.vo.AppointmentVo;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.Appointment;
import com.ruoyi.system.service.IAppointmentService;

/**
 * 预约挂号Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-14
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private HospitalAreaMapper hospitalAreaMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private JobTitleMapper jobTitleMapper;

    @Override
    public List<AppointmentVo> selectAppointmentList(AppointmentVo appointmentVo) {
        List<AppointmentVo> list = appointmentMapper.selectAppointmentList(appointmentVo);
        return list;
    }

    @Override
    public Appointment selectAppointmentById(Long id) {
        Appointment appointment = appointmentMapper.selectAppointmentById(id);
        if (appointment != null) {
            // 设置医院名称
            if (appointment.getHospitalId() != null) {
                String hospitalName = hospitalMapper.selectById(appointment.getHospitalId());
                if (hospitalName != null) {
                    appointment.setHospitalName(hospitalName);
                }
            }
            
            // 设置医院地址
            if (appointment.getHospitalAddress() != null) {
                String areaName = hospitalAreaMapper.selectById(appointment.getHospitalAddress());
                if (areaName != null) {
                    appointment.setHospitalAddressName(areaName);
                }
            }
            
            // 设置医生和科室信息
            if (appointment.getDoctorId() != null) {
                Doctor doctor = doctorMapper.selectDoctorById(appointment.getDoctorId());
                if (doctor != null) {
                    appointment.setDoctorName(doctor.getName());
                    
                    // 设置科室名称
                    if (doctor.getDepartmentId() != null) {
                        String deptName = departmentMapper.selectById(doctor.getDepartmentId());
                        if (deptName != null) {
                            appointment.setDepartmentName(deptName);
                        }
                    }
                    
                    // 设置职称
                    if (doctor.getTitleId() != null) {
                        String titleName = jobTitleMapper.selectById(doctor.getTitleId());
                        if (titleName != null) {
                            appointment.setJobTitleName(titleName);
                        }
                    }
                }
            }
        }
        return appointment;
    }

}
